use std::sync::Arc;

use prisma_client_rust::{QueryError, WhereQuery, Direction};
use serde::{Serialize, Deserialize};

use crate::prisma::{PrismaClient, action_record::{Data, self}};
use rspc::Type;

#[derive(Clone, Serialize, Deserialize, Type, Debug)]
pub struct ActionRecordParams {
    pub start: Option<String>,
    pub end: Option<String>,
    pub types: Option<String>
}

pub struct ActionManager {}

impl ActionManager {
    pub async fn get(client: Arc<PrismaClient>, args: ActionRecordParams) -> Result<Vec<Data>, QueryError> {
        let mut query = client
            .action_record()
            .find_many(vec![]);

        if !args.start.is_none() {
            query
                .add_where(action_record::created_at::gte(args.start.unwrap().to_string()));
        }

        if !args.end.is_none() {
            query
                .add_where(action_record::created_at::lte(args.end.unwrap().to_string()));
        }

        if !args.types.is_none() {
            query
                .add_where(action_record::types::equals(Some(args.types.unwrap().to_string())));
        }

        Ok(
            query
                .order_by(action_record::created_at::order(Direction::Desc))
                .exec()
                .await?
        )
    }
}
